/**
 * 
 */
package com.moioo.exam;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.moioo.util.PageAuger;

/**
 * @author 3400423@qq.com
 *
 */
public class TSubjectDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TSubjectDAO.class);
	public void save(TSubject transientInstance) {
		log.debug("saving TSubject instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TSubject persistentInstance) {
		log.debug("deleting TSubject instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TSubject findById(java.lang.Integer id) {
		log.debug("getting TSubject instance with id: " + id);
		try {
			TSubject instance = (TSubject) getSession().get(
					"com.moioo.exam.TSubject", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TSubject instance) {
		log.debug("finding TSubject instance by example");
		try {
			List results = getSession().createCriteria(
					"com.moioo.exam.TSubject").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TSubject instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TSubject as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public List<TSubject> findSon(String ParentId) {
		log.debug("finding all TSubject instances");
		try {
			String whereString = "";
			String queryString = "from TSubject t ";
			whereString += " AND t.id in(select t.id from TSubject as t where t.parent like '%," + ParentId+",%')";
			if(whereString.indexOf("AND") != -1)
				whereString = " WHERE " + whereString.substring(4);
			queryString += whereString;
			Query rows = getSession().createQuery("select count(t.id) from TSubject as t " + whereString);
			queryString += " order by t.id desc";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<TSubject> findAll() {
		log.debug("finding all TSubject instances");
		try {
			String queryString = "from TSubject as t order by t.id asc";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public TSubject merge(TSubject detachedInstance) {
		log.debug("merging TSubject instance");
		try {
			TSubject result = (TSubject) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TSubject instance) {
		log.debug("attaching dirty TSubject instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TSubject instance) {
		log.debug("attaching clean TSubject instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}	
}
